package com.leetCode.solution1;

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        while (head != null && head.val == val) {
            head = head.next;
        }

        if (head == null) {
            return null;
        }


        ListNode preNode = head;
        while (preNode.next != null) {
            if (preNode.next.val == val) {
                preNode.next =  preNode.next.next;
            }else{
                preNode = preNode.next;
            }
        }

        return head;
    }
}